package com.wwj.auction.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.auction.admin.entity.*;
import com.wwj.auction.admin.entity.vo.*;
import com.wwj.auction.admin.enums.SellerContactsRelation;
import com.wwj.auction.admin.mapper.SellerAuditImageMapper;
import com.wwj.auction.admin.mapper.SellerAuditMapper;
import com.wwj.auction.admin.mapper.UserAddressMapper;
import com.wwj.auction.admin.mapper.UserInfoMapper;
import com.wwj.auction.admin.service.SellerAuditService;
import com.wwj.auction.common.util.SystemUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-08-26
 */
@Service
public class SellerAuditServiceImpl extends ServiceImpl<SellerAuditMapper, SellerAudit> implements SellerAuditService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private SellerAuditImageMapper sellerAuditImageMapper;
    @Resource
    private UserAddressMapper userAddressMapper;
    @Resource
    private SellerAuditMapper sellerAuditMapper;

    @Override
    public PageModel<SellerAuditVo> list(Integer page, Integer limit, String keyword) {
        LambdaQueryWrapper<SellerAudit> wrapper = Wrappers.lambdaQuery(SellerAudit.class);
        // 判断是否有查询条件
        if (keyword != null) {
            // 拼接查询条件
            wrapper.like(SellerAudit::getStoreName, keyword)
                    .or()
                    .like(SellerAudit::getPhone, keyword)
                    .or()
                    .like(SellerAudit::getContactsMobile, keyword)
                    .or()
                    .like(SellerAudit::getIdCard, keyword);
        }
        // 按id降序排序，以优先显示最新的数据
        wrapper.orderByDesc(SellerAudit::getId);
        // 执行分页查询流程
        Page<SellerAudit> sellerAuditPage = new Page<>(page, limit);
        List<SellerAudit> sellerAuditList = baseMapper.selectPage(sellerAuditPage, wrapper).getRecords();
        // 封装Vo数据
        List<SellerAuditVo> sellerAuditVoList = sellerAuditList.stream().map(sellerAudit -> {
            SellerAuditVo sellerAuditVo = new SellerAuditVo();
            // 属性拷贝
            BeanUtils.copyProperties(sellerAudit, sellerAuditVo);
            // 处理日期时间
            LocalDateTime localDateTime = sellerAudit.getCreateTime();
            String time = SystemUtil.parseDate(localDateTime);
            sellerAuditVo.setCreateTime(time);
            return sellerAuditVo;
        }).collect(Collectors.toList());
        // 封装最终结果
        PageModel<SellerAuditVo> pageModel = new PageModel<>();
        pageModel.setList(sellerAuditVoList);
        pageModel.setTotal(sellerAuditPage.getTotal());
        return pageModel;
    }

    @Override
    public SellerDetailVO show(Integer id) {
        SellerDetailVO sellerDetailVO = new SellerDetailVO();
        // 查询得到基本信息
        SellerAudit sellerAudit = baseMapper.selectById(id);
        Integer userId = sellerAudit.getUserId();
        // 通过userId获取基本信息
        UserInfo userInfo = userInfoMapper.selectById(userId);
        // 属性拷贝（包括姓名、性别、手机号、地址、身份证号）
        BeanUtils.copyProperties(userInfo, sellerDetailVO);
        // 设置审核结果
        sellerDetailVO.setStatus(sellerAudit.getStatus());
        // 处理地址信息
        Integer addressId = userInfo.getAddress();
        // 查询地址信息
        UserAddress userAddress = userAddressMapper.selectById(addressId);
        // 拼接地址内容
        StringBuilder sb = new StringBuilder();
        sb.append(userAddress.getProvince());
        sb.append(userAddress.getCity());
        sb.append(userAddress.getCounty());
        sellerDetailVO.setAddress(sb.toString());
        // 设置店铺名
        sellerDetailVO.setStoreName(sellerAudit.getStoreName());
        // 设置创建时间
        LocalDateTime localDateTime = sellerAudit.getCreateTime();
        String time = SystemUtil.parseDate(localDateTime);
        sellerDetailVO.setCreateTime(time);
        // 设置联系人
        sellerDetailVO.setContactsName(sellerAudit.getContactsName());
        // 设置联系人手机号
        sellerDetailVO.setContactsMobile(sellerAudit.getContactsMobile());
        // 设置联系人关系
        Integer relation = sellerAudit.getContactsRelation();
        if (relation.equals(SellerContactsRelation.SELLER_PARENTS.getStatus())) {
            sellerDetailVO.setContactsRelation(SellerContactsRelation.SELLER_PARENTS.getMsg());
        } else if (relation.equals(SellerContactsRelation.SELLER_SPOUSE.getStatus())) {
            sellerDetailVO.setContactsRelation(SellerContactsRelation.SELLER_SPOUSE.getMsg());
        } else if (relation.equals(SellerContactsRelation.SELLER_OTHER.getStatus())) {
            sellerDetailVO.setContactsRelation(SellerContactsRelation.SELLER_OTHER.getMsg());
        }
        // 处理审核材料中的图片
        Integer sellerAuditId = sellerAudit.getId();
        // 通过该id查询出图片信息
        List<SellerAuditImage> sellerAuditImageList = sellerAuditImageMapper.selectList(
                Wrappers.lambdaQuery(SellerAuditImage.class)
                        .eq(SellerAuditImage::getSellerAuditId, sellerAuditId));
        List<SellerAuditImageVo> sellerAuditImageVoList = sellerAuditImageList.stream().map(sellerAuditImage -> {
            SellerAuditImageVo sellerAuditImageVo = new SellerAuditImageVo();
            BeanUtils.copyProperties(sellerAuditImage, sellerAuditImageVo);
            return sellerAuditImageVo;
        }).collect(Collectors.toList());
        sellerDetailVO.setSellerAttachVOList(sellerAuditImageVoList);
        return sellerDetailVO;
    }
}
